﻿using CaterBll;
using CaterModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaterUI
{
    public partial class FormManagerInfo : Form
    {
      
        private FormManagerInfo()
        {
            InitializeComponent();
        }

        //实现窗体的单例模式
        private static FormManagerInfo _form;
        public static FormManagerInfo Create()
        {
            if(_form==null)
            {
                _form = new FormManagerInfo();
            }
            return _form;
        }
        

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void FormManagerInfo_Load(object sender, EventArgs e)
        {
            LoadList();
        }
        ManagerInfoBll miBll = new ManagerInfoBll();
        private void LoadList()
        {
            
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = miBll.GetList();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //接收用户输入
            ManagerInfo mi = new ManagerInfo()
            {
                MName = txtName.Text,
                MPwd = txtPwd.Text,
                MType = rb1.Checked ? 1 : 0
            };


            if (txtId.Text == "添加时无编号")
            {
                #region 添加
                if (miBll.Add(mi))
                {
                    LoadList();
                    //清除值
                    txtName.Text = "";
                    txtPwd.Text = "";
                    rb2.Checked = false;

                }
                else
                {
                    MessageBox.Show("添加失败，请稍后重试！");
                }
                #endregion

            }
            else
            {
                #region 修改
                mi.MId =int.Parse(txtId.Text);
                if(miBll.Edit(mi))
                {
                    LoadList();
                }
                 
                #endregion
            }

            //清除文本的值
            CleanData();
        }

        private void dgvList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //对类型列进行格式化处理
           if( e.ColumnIndex==2)
            {
                e.Value = Convert.ToInt32(e.Value) == 1 ? "经理" : "店员";
            }
        }

        //修改
        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dgvList.Rows[e.RowIndex];

            txtId.Text = row.Cells[0].Value.ToString();
            txtName.Text = row.Cells[1].Value.ToString();
            if(row.Cells[2].Value.ToString().Equals("1"))
            {
                rb1.Checked = true;//值为1，则经理选中
            }
            else
            {
                rb2.Checked = true;
            }
            txtPwd.Text = "这是原来的密码吗";
            btnSave.Text = "修改";

        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            //删除前确认
           DialogResult result=MessageBox.Show("你确定要删除吗？", "删除确认", MessageBoxButtons.OKCancel);
            if(result==DialogResult.Cancel)
            {
                return;
            }
            //获取选中的行
            var rows = dgvList.SelectedRows;
            if(rows.Count>0)
            {
               int id=int.Parse(rows[0].Cells[0].Value.ToString());
               if( miBll.Remove(id))
                {
                    LoadList();
                }
            }
            else
            {
                MessageBox.Show("请先选择要删除的行！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CleanData();
        }

        private void CleanData()
        {
            //清除文本的值
            txtId.Text = "添加时无编号";
            txtName.Text = "";
            txtPwd.Text = "";
            rb2.Checked = true;
            btnSave.Text = "添加";
        }

        private void FormManagerInfo_FormClosed(object sender, FormClosedEventArgs e)
        {
            _form = null;
        }
    }
}
